package com.zsh.wanandroid.ui.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dxq.basic_ui_library.loadSirCallBack.EmptyCallback;
import com.dxq.basic_ui_library.loadSirCallBack.ErrorCallback;
import com.dxq.basic_ui_library.loadSirCallBack.LoadingCallback;
import com.kingja.loadsir.core.LoadSir;
import com.zsh.wanandroid.R;
import com.zsh.wanandroid.api.ApiService;
import com.zsh.wanandroid.base.BaseActivity;
import com.zsh.wanandroid.bean.ScoreResult;
import com.zsh.wanandroid.http.ApiRequest;
import com.zsh.wanandroid.ui.adapter.ScoreAdapter;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author zsh
 * @date 2020/3/12.
 * 功能：积分页面
 */

public class ScoreActivity extends BaseActivity {

    private ImageView ivBack;
    private TextView tvTitle;
    private ImageView ivMore;
    private TextView tvScore;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;

    private ScoreAdapter scoreAdapter;

    private String userScore = "";

    private static final int DEFAULT_PAGE = 1;
    private int currentPage = DEFAULT_PAGE;//默认加载第一页
    private int pageCount;

    @Override
    public void initPrams(Bundle prams) {
        if (null != prams) {
            userScore = prams.getString("scoreUser");
        }
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_score;
    }

    @Override
    public void initView(View view) {
        ivBack = $(R.id.ivBack);
        tvTitle = $(R.id.tvTitle);
        ivMore = $(R.id.ivMore);
        tvScore = $(R.id.tvScore);
        swipeRefreshLayout = $(R.id.swipeRefreshLayout);
        recyclerView = $(R.id.recyclerView);
    }

    @Override
    public void setListener() {
        ivBack.setOnClickListener(this);
        ivMore.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.ivBack://返回
                back();
                break;

            case R.id.ivMore://排名
                //startActivity(RankActivity.class);
                break;
        }
    }

    @Override
    public void doBusiness(Bundle savedInstanceState) {
        //设置标题
        tvTitle.setText("积分明细");
        tvScore.setText(userScore);
        ivMore.setImageResource(R.drawable.ic_help_white_24dp);
        initLoadSir();
        initRecyclerView();
        initSwipeRefreshLayout();
        getScore();
    }

    private void initRecyclerView() {
        //添加Android自带的分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        scoreAdapter = new ScoreAdapter(R.layout.score_item);
        recyclerView.setAdapter(scoreAdapter);
    }

    private void initSwipeRefreshLayout() {
        //下拉刷新
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = DEFAULT_PAGE;
                getScore();
            }
        });

        //上拉加载更多
        scoreAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage++;
                getScore();
            }
        });
    }

    private void initLoadSir() {
        loadService = LoadSir.getDefault().register(swipeRefreshLayout, new com.kingja.loadsir.callback.Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                currentPage = DEFAULT_PAGE;
                getScore();
            }
        });
    }

    private void back() {
        self.finish();
    }

    /**
     * 获取个人积分信息
     */
    private void getScore() {
        ApiService apiService = ApiRequest.getInstance(self).create(ApiService.class);
        apiService.getUserScoreList(currentPage).enqueue(new Callback<ScoreResult>() {
            @Override
            public void onResponse(Call<ScoreResult> call, Response<ScoreResult> response) {
                swipeRefreshLayout.setRefreshing(false);
                ScoreResult body = response.body();
                if (null == body) {
                    loadService.showCallback(EmptyCallback.class);
                    return;
                }
                ScoreResult.Data data = body.getData();
                currentPage = data.getCurPage();
                pageCount = data.getPageCount();
                if (null == data) {
                    loadService.showCallback(EmptyCallback.class);
                    return;
                }
                List<ScoreResult.Data.Datas> datas = data.getDatas();
                if (null != datas && datas.size() > 0) {
                    loadService.showSuccess();
                    if (currentPage == DEFAULT_PAGE) {
                        scoreAdapter.replaceData(datas);
                    } else {
                        scoreAdapter.addData(datas);
                    }
                    if (currentPage < pageCount) {
                        scoreAdapter.loadMoreComplete();
                    } else {
                        scoreAdapter.loadMoreEnd();
                    }
                } else {
                    loadService.showCallback(EmptyCallback.class);
                }
            }

            @Override
            public void onFailure(Call<ScoreResult> call, Throwable t) {
                swipeRefreshLayout.setRefreshing(false);
                loadService.showCallback(ErrorCallback.class);
                currentPage = DEFAULT_PAGE;
            }
        });

    }

    @Override
    public void onBackPressed() {
        back();
    }
}
